package cc.sheji;

import cc.sheji.config.DeviceConfig;
import cc.sheji.domain.User;
import cc.sheji.reposity.UserRepository;
import cc.sheji.service.JobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;


@EnableScheduling
@SpringBootApplication
public class TaskApplication {

    @Autowired
    UserRepository userRepository;

    @Autowired
    JobService jobService;
    @Autowired
    DeviceConfig deviceConfig;

    public static void main(String[] args) {
        SpringApplication.run(TaskApplication.class, args);
    }

    boolean isDoTask2;

    @Scheduled(fixedRate = 500000)
    public void doTask2() {

//        if(isDoTask2){
//         //   System.out.println("have done task2" );
//            return;
//        }
//        isDoTask2=true;
//        System.out.println("start task add user level" );
//        List<User> all = userRepository.findAll(null);
//        for(int i=0;i<all.size();i++){
//            User user=all.get(i);
//            user.setLevel(0);
//            userRepository.save(user);
//            System.out.println("do task add user level"+i+"/"+all.size());
//        }
//        System.out.println("end task add user level" );
    }


    @Scheduled(fixedRate = 5000)
    public void doTask() {
        try {
            if(deviceConfig.mode==1)
            jobService.doTask();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        // System.out.println("do task");
    }

    @Scheduled(fixedRate = 5000)
    public void redoTask() {
        try {
            if(deviceConfig.mode==1)
                jobService.task();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        // System.out.println("re do task");
    }

//    @Scheduled(fixedRate = 5000)
//    public void nextDoTask() {
//        try {
//            if(deviceConfig.mode==1)
//            jobService.nextDoTask();
//        } catch (Throwable throwable) {
//            throwable.printStackTrace();
//        }
//        // System.out.println("re do task");
//    }

//    @Scheduled(fixedRate = 5000)
//    public void nextDoVTask() {
//        try {
//            if(deviceConfig.mode==1)
//            jobService.nextDoVTask();
//        } catch (Throwable throwable) {
//            throwable.printStackTrace();
//        }
//        // System.out.println("re do task");
//    }

//    @Scheduled(fixedRate = 40000)
//    public void queryTask() throws Exception {
//        try {
//            if(deviceConfig.mode==2)
//            jobService.queryTask();
//        } catch (Throwable throwable) {
//            throwable.printStackTrace();
//        }
//    }

    @Scheduled(fixedRate = 5000)
    public void download() throws Exception {
        try {
            if(deviceConfig.mode==1)
            jobService.download();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        // System.out.println("download");

    }


    @Scheduled(fixedRate = 60000)
    public void noticeError() throws Exception {
        try {
            //if(deviceConfig.mode==1)
            jobService.noticeErrorTask();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        // System.out.println("download");
    }
//
    @Scheduled(fixedRate = 60000)
    public void clear() throws Exception {
        try {
            //if(deviceConfig.mode==1)
            jobService.cleanErrorTask();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        // System.out.println("download");
    }
}
